package com.siyuanren.gcocp.service.impl;

import com.siyuanren.gcocp.dto.AdminQueryDTO;
import com.siyuanren.gcocp.mapper.MyAdminMapper;
import com.siyuanren.gcocp.service.AdminService;
import com.siyuanren.gcocp.vo.*;
import org.apache.http.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * Created by   anxing
 * Project_name gcocp-parent
 * Path         com.siyuanren.gcocp.service.impl
 * Date         2018/5/2
 * Time         11:30
 * Description
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    MyAdminMapper adminMapper;

    @Override
    public void insert(AdminInsertVo vo) throws IOException {
        adminMapper.insert(buildInsertSql(vo));
    }
        private SqlVo buildInsertSql(AdminInsertVo insertVo) {
        String sql = "INSERT INTO wx_course VALUES('0','2018-04-02 10:00:41','1','1','1','1','1','1','1','1','1','1','1','1','1')";
        StringBuffer col = new StringBuffer("INSERT INTO ").append(insertVo.getTableName()).append(" (id,");
        StringBuffer values = new StringBuffer(" VALUES('0',");
        for (String key : insertVo.getValues().keySet()) {
            col.append(key).append(",");
            values.append("'").append(insertVo.getValues().get(key)).append("',");
        }
        col.deleteCharAt(col.length()-1);
        values.deleteCharAt(values.length()-1);
        col.append(")");
        values.append(")");
        sql = col.toString() + values.toString();

        SqlVo vo = new SqlVo();
        vo.setSql(sql);
        return vo;
    }
    @Override
    public void delete(AdminIdVo vo) throws IOException {
        String sql = "DELETE FROM " + vo.getTableName() + " WHERE id = " + vo.getId();
        SqlVo sqlVo = new SqlVo();
        sqlVo.setSql(sql);
        adminMapper.delete(sqlVo);
    }

    @Override
    public void update(AdminUpdateVo vo) throws IOException {
        SqlVo sqlVo = new SqlVo();
        sqlVo.setSql(buildUpdateSql(vo));
        adminMapper.update(sqlVo);
    }
    private String buildUpdateSql(AdminUpdateVo updateVo) {
        StringBuffer col = new StringBuffer("update ").append(updateVo.getTableName()).append(" set ");
        for (String key : updateVo.getValues().keySet()) {
            col.append(key).append("=");
            col.append("'").append(updateVo.getValues().get(key)).append("'").append(",");

        }
        col.deleteCharAt(col.length()-1);
        col.append(" where id =" + updateVo.getId());

        return col.toString();
    }
    @Override
    public AdminQueryDTO select(AdminQueryVo vo) throws IOException {
        StringBuffer countsql = new StringBuffer("select count(*) from ").append(vo.getTableName());
        StringBuffer sql = new StringBuffer("select * from ").append(vo.getTableName());
        if (!TextUtils.isEmpty(vo.getId()) || (!TextUtils.isEmpty(vo.getSearchKey()) && !TextUtils.isEmpty(vo.getSearchValue()))) {
            sql.append(" where ");
            countsql.append(" where ");
        }
        if (vo.getId() != null) {
            sql.append("id=").append(vo.getId());
            countsql.append("id=").append(vo.getId());
        }
        if (!TextUtils.isEmpty(vo.getId()) && !TextUtils.isEmpty(vo.getSearchKey()) && !TextUtils.isEmpty(vo.getSearchValue())) {
            sql.append(" and ");
            countsql.append(" and ");
        }
        if (!TextUtils.isEmpty(vo.getSearchKey()) && !TextUtils.isEmpty(vo.getSearchValue())) {
            sql.append(vo.getSearchKey()).append(" like '%").append(vo.getSearchValue()).append("%'");
            countsql.append(vo.getSearchKey()).append(" like '%").append(vo.getSearchValue()).append("%'");
        }
        sql.append(" ORDER BY id DESC limit ").append(vo.getCurrPage() * vo.getPageSize()).append(",").append(vo.getPageSize());

        AdminQueryDTO queryDTO = new AdminQueryDTO();
        // 查询数据条数
        SqlVo sqlVo = new SqlVo();
        sqlVo.setSql(countsql.toString());
        queryDTO.setTotal(adminMapper.selectCount(sqlVo));

        // 查询数据
        sqlVo.setSql(sql.toString());
        queryDTO.setDatas(adminMapper.select(sqlVo));
        return queryDTO;
    }
}
